package com.kedll.library.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.kedll.library.adapter.MyArrayAdapter;
import com.kedll.library.util.Parse;

import java.util.ArrayList;
import java.util.Map;

public abstract class MyBaseLetterAdapter extends MyArrayAdapter implements
        SectionIndexer {

    protected ArrayList<Map<String, Object>> list;// 数据
    protected LayoutInflater getInflater;// 加载布局

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public MyBaseLetterAdapter(Context context) {
        super(context, 0);
        this.getInflater = LayoutInflater.from(context);
    }


    /**
     * 转型用
     *
     * @return
     */
    protected Parse getParse() {
        return Parse.getInstance();
    }

    /**
     * 设置textview，可以是HTML文本
     *
     * @param key       集合中的Map的key
     * @param position  当前位置
     * @param textViews 需要设置的TextView，个数与第一个参数的长度一致，并且设置的要一一对应
     */
    @SuppressWarnings("unchecked")
    protected void displayText(String[] key, int position,
                               TextView... textViews) {
        for (int i = 0; i < textViews.length; i++) {
            textViews[i].setText(Html.fromHtml(getParse().isNull(
                    list.get(position).get(key[i]))));
        }
    }

    @Override
    public Object[] getSections() {
        // TODO Auto-generated method stub
        return null;
    }

    @SuppressLint("DefaultLocale")
    @Override
    public int getPositionForSection(int section) {
        // TODO Auto-generated method stub
        for (int i = 0; i < getCount(); i++) {
            @SuppressWarnings("unchecked")
            String sortStr = getParse().isNull(list.get(i).get("sortLetters"));
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    @SuppressWarnings("unchecked")
    @Override
    public int getSectionForPosition(int position) {
        // TODO Auto-generated method stub
        return getParse().isNull(list.get(position).get("sortLetters")).charAt(
                0);
    }

}
